/* UniApo: Section.h
 * filename:  Section
 * date:      April 2010
 * author:    Jeffrey Geyssens
 * about:     This file contains implementation of the a basic Section.
 *            It is the container by choice for representing data in
 * Marked as completed: 10th june 2010 and no changes since
 */
#ifndef _SECTION_H
#define	_SECTION_H
#include <iostream>
#include <vector>
using namespace std;

template <typename T>
class Section {
private:

    vector<T> container;
    string header;
    string footer;
    int size;


public: /*Sections are unsorted containers.
         * Therefore it is only needed to add entries to the back
         * or modifying entries */

    int getSize();

    void add(T const &d); //add to back of container 0(1)
    //performance hit if more space needed
    void insert(T const &d, int entryNo); //O(n) due to storage move
    void modify(T const &d, int entryNo); //O(1)
    void remove(int entryNo); //O(n) due storage move
    T get(int entryNo);
    void print(); //O(n)
    void buildFile(string filename); //0(n)
    void test();
    //T getObject(int entryNo);

    vector<T>& getSectionData() {
        return container;
    }

    void setFooter(const string& footer) {
        this->footer = footer;
    }

    void setHeader(const string& header) {
        this->header = header;
    }

    const string& getFooter() const {
        return footer;
    }

    const string& getHeader() const {
        return header;
    }


};

template <typename T> int Section<T>::getSize() {
    return container.size();

}

template <typename T> void Section<T>::add(T const &d) {
    container.push_back(d);
    size += 1;
}

template <typename T> void Section<T>::insert(T const &d, int entryNo) {
    entryNo -= 1;
    if ((entryNo >= container.size()) || (entryNo < 0)) {
        cerr << "Error (Section): entry number out of bounds" << endl;

    } else {
        container.insert(container.begin() + entryNo, d);
        size += 1;
    }
}

template <typename T> void Section<T>::modify(T const &d, int entryNo) {
    entryNo -= 1;
    if ((entryNo >= container.size()) || (entryNo < 0)) {
        cerr << "Error (Section): entry number out of bounds" << endl;

    } else {
        
        container[entryNo] = d;
    }
}

template <typename T> T Section<T>::get(int entryNo) {
    entryNo -= 1;
    if ((entryNo >= container.size()) || (entryNo < 0)) {
        cerr << "Error (Section): entry number out of bounds" << endl;
    }
    return container[entryNo];
}

template <typename T> void Section<T>::remove(int entryNo) {
    entryNo -= 1;
    if ((entryNo >= size) || (entryNo < 0)) {
        cerr << "Error (Section): entry number out of bounds" << endl;

    }
    container.erase(container.begin() + entryNo);
    size = size - 1;
}

template <typename T> void Section<T>::print() {
    cout << header << endl;
    typename vector<T>::iterator It1;
    int i = 1;
    It1 = container.begin();
    for (It1 = container.begin(); It1 != container.end(); ++It1) {
        cout << i << ". " << *It1 << endl;
        i++;
    }
    cout << footer << endl;
}

template <typename T> void Section<T>::buildFile(string filename) {
    ofstream myfile(filename.c_str(), ios::app);

    myfile << header << "\n";
    typename vector<T>::iterator It1;
    It1 = container.begin();
    for (It1 = container.begin(); It1 != container.end(); ++It1)
        myfile << *It1 << "\n";
    myfile << footer << "\n";
}

#endif	/* _SCBFILE_H */